﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Views.Grid;
using GeneralDefine;

namespace TableModule
{
    public partial class FormDebugSimple : DevExpress.XtraEditors.XtraForm
    {
        public FormDebugSimple()
        {
            InitializeComponent();
        }
        private void FormDebugSimple_Load(object sender, EventArgs e)
        {
            comboBoxEditMode.SelectedIndex = 0;
            comboBoxEditDistance.SelectedIndex = 0;

            UpDateComBox();
            timer1.Enabled = true;
        }

        
        private void UpDateComBox()
        {

            //comboBoxTool.Items.Add(actioner.tableDate.listTableDataTool[0].strName);
            comboBoxEditTool.Properties.Items.Add("Default");
            foreach (TableDataTool itemTool in TableManager.globaltableData.TableSingleData.listTableDataTool)
            {
                comboBoxEditTool.Properties.Items.Add(itemTool.strName);
            }
            comboBoxEditLocal.Properties.Items.Add("Default");
            foreach (TableDataLocal itemLocal in TableManager.globaltableData.TableSingleData.listTableDataLocal)
            {
                comboBoxEditLocal.Properties.Items.Add(itemLocal.strName);
            }

            comboBoxEditTool.Text = TableManager.tableActioner.toolSetting.strName;
            comboBoxEditLocal.Text = TableManager.tableActioner.localSetting.strName;

        }
        private void timerStatusScan_Tick(object sender, EventArgs e)
        {
            
        }

        private void comboBoxEditTool_SelectedValueChanged(object sender, EventArgs e)
        {
            TableManager.tableActioner.SetTool(comboBoxEditTool.Text);
        }

        private void comboBoxEditLocal_SelectedValueChanged(object sender, EventArgs e)
        {
            TableManager.tableActioner.SetLocal(comboBoxEditLocal.Text);
        }

        private void simpleButtonCWY_MouseDown(object sender, MouseEventArgs e)
        {
            SimpleButton simpleButton = sender as SimpleButton;
            if (simpleButton != null)
            {
                TableManager.tableActioner.tableMotionSpeed.dStartSpeed = ((double)zoomTrackBarControlSpd.Value / 10.0);
                TableManager.tableActioner.tableMotionSpeed.dMoveSpeed = zoomTrackBarControlSpd.Value;
                TableManager.tableActioner.tableMotionSpeed.dEndSpeed = ((double)zoomTrackBarControlSpd.Value / 10.0);
                TableManager.tableActioner.tableMotionSpeed.dAcc = TableManager.tableActioner.tableMotionSpeed.dMoveSpeed * TableManager.tableActioner.tableMotionSpeed.dMoveSpeed * 10;
                TableManager.tableActioner.tableMotionSpeed.dDec = TableManager.tableActioner.tableMotionSpeed.dMoveSpeed * TableManager.tableActioner.tableMotionSpeed.dMoveSpeed * 10;
                TableManager.tableActioner.SetSpd(TableManager.tableActioner.tableMotionSpeed);
                if (simpleButton.Text == "X+")
                {
                    if (comboBoxEditMode.SelectedIndex == 0)
                    {

                        TableManager.tableActioner.Jog(AxisName.X, MotionDir.CW);
                    }
                    else
                    {
                        double dPos = 0.0;
                        bool bParseOk = double.TryParse(comboBoxEditDistance.Text, out dPos);
                        if (bParseOk)
                        {
                            TableManager.tableActioner.RelMove(AxisName.X, dPos);
                        }
                        else
                        {
                            comboBoxEditMode.SelectedIndex = 0;
                        }
                    }
                }
                if (simpleButton.Text == "X-")
                {
                    if (comboBoxEditMode.SelectedIndex == 0)
                    {

                        TableManager.tableActioner.Jog(AxisName.X, MotionDir.CCW);
                    }
                    else
                    {
                        double dPos = 0.0;
                        bool bParseOk = double.TryParse(comboBoxEditDistance.Text, out dPos);
                        if (bParseOk)
                        {
                            TableManager.tableActioner.RelMove(AxisName.X, -dPos);
                        }
                        else
                        {
                            comboBoxEditMode.SelectedIndex = 0;
                        }
                    }
                }
                if (simpleButton.Text == "Y+")
                {
                    if (comboBoxEditMode.SelectedIndex == 0)
                    {

                        TableManager.tableActioner.Jog(AxisName.Y, MotionDir.CW);
                    }
                    else
                    {
                        double dPos = 0.0;
                        bool bParseOk = double.TryParse(comboBoxEditDistance.Text, out dPos);
                        if (bParseOk)
                        {
                            TableManager.tableActioner.RelMove(AxisName.Y, dPos);
                        }
                        else
                        {
                            comboBoxEditMode.SelectedIndex = 0;
                        }
                    }
                }
                if (simpleButton.Text == "Y-")
                {
                    if (comboBoxEditMode.SelectedIndex == 0)
                    {

                        TableManager.tableActioner.Jog(AxisName.Y, MotionDir.CCW);
                    }
                    else
                    {
                        double dPos = 0.0;
                        bool bParseOk = double.TryParse(comboBoxEditDistance.Text, out dPos);
                        if (bParseOk)
                        {
                            TableManager.tableActioner.RelMove(AxisName.Y, -dPos);
                        }
                        else
                        {
                            comboBoxEditMode.SelectedIndex = 0;
                        }
                    }
                }
                if (simpleButton.Text == "Z+")
                {
                    if (comboBoxEditMode.SelectedIndex == 0)
                    {

                        TableManager.tableActioner.Jog(AxisName.Z, MotionDir.CW);
                    }
                    else
                    {
                        double dPos = 0.0;
                        bool bParseOk = double.TryParse(comboBoxEditDistance.Text, out dPos);
                        if (bParseOk)
                        {
                            TableManager.tableActioner.RelMove(AxisName.Z, dPos);
                        }
                        else
                        {
                            comboBoxEditMode.SelectedIndex = 0;
                        }
                    }
                }
                if (simpleButton.Text == "Z-")
                {
                    if (comboBoxEditMode.SelectedIndex == 0)
                    {

                        TableManager.tableActioner.Jog(AxisName.Z, MotionDir.CCW);
                    }
                    else
                    {
                        double dPos = 0.0;
                        bool bParseOk = double.TryParse(comboBoxEditDistance.Text, out dPos);
                        if (bParseOk)
                        {
                            TableManager.tableActioner.RelMove(AxisName.Z, -dPos);
                        }
                        else
                        {
                            comboBoxEditMode.SelectedIndex = 0;
                        }
                    }
                }
                if (simpleButton.Text == "U+")
                {
                    if (comboBoxEditMode.SelectedIndex == 0)
                    {

                        TableManager.tableActioner.Jog(AxisName.U, MotionDir.CW);
                    }
                    else
                    {
                        double dPos = 0.0;
                        bool bParseOk = double.TryParse(comboBoxEditDistance.Text, out dPos);
                        if (bParseOk)
                        {
                            TableManager.tableActioner.RelMove(AxisName.U, dPos);
                        }
                        else
                        {
                            comboBoxEditMode.SelectedIndex = 0;
                        }
                    }
                }
                if (simpleButton.Text == "U-")
                {
                    if (comboBoxEditMode.SelectedIndex == 0)
                    {

                        TableManager.tableActioner.Jog(AxisName.U, MotionDir.CCW);
                    }
                    else
                    {
                        double dPos = 0.0;
                        bool bParseOk = double.TryParse(comboBoxEditDistance.Text, out dPos);
                        if (bParseOk)
                        {
                            TableManager.tableActioner.RelMove(AxisName.U, -dPos);
                        }
                        else
                        {
                            comboBoxEditMode.SelectedIndex = 0;
                        }
                    }
                }
            }
        }

        private void simpleButtonCWU_MouseUp(object sender, MouseEventArgs e)
        {
            if (comboBoxEditMode.SelectedIndex == 0)
            {

                TableManager.tableActioner.StopALL();
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            TableDataPosition posCurrent = TableManager.tableActioner.WhereTable();
            textEditXPos.Text = posCurrent.dXPos.ToString() ;
            textEditYPos.Text = posCurrent.dYPos.ToString();
            textEditZPos.Text = posCurrent.dZPos.ToString();
            textEditUPos.Text = posCurrent.dUPos.ToString();
        }
    }
}